<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 300px"
        placeholder="请输入盘点单编码"
        v-model="search.code"
        clearable
      >
      </el-input>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        class="hidden-xs-only"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >

      <el-button
        class="add"
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addPermission"
        >添加</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出
      </el-button>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="code"
        label="code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="admin_name"
        label="添加人"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label="添加时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="examine_admin_name"
        label="审核人"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="examine_time"
        label="审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_code"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="280px">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="danger"
            @click="remove(scope)"
            icon="el-icon-delete"
            >删除</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.status == 1"
            @click="modification(scope)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- dialog -->
    <addSuperCheckDialog
      :superDialog="superDialog"
      @upData="getData"
    ></addSuperCheckDialog>
    <particularsCheckDialog
      :particularsDialog="particularsDialog"
    ></particularsCheckDialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import { getnowDate } from "@/utils/index";
import AddSuperCheckDialog from "@/components/check/addSuperCheckDialog";
import ParticularsCheckDialog from "@/components/check/particularsCheckDialog";

import Pagination from "@/components/pagination";
import { stockList, stockDelete, stockCheck } from "@/api/check/superCheck";
export default {
  components: {
    pagination: Pagination,
    addSuperCheckDialog: AddSuperCheckDialog,
    particularsCheckDialog: ParticularsCheckDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      search: {
        status: "",
        code: "",
        created_date: "",
        end_date: "",
      },
      normal_endtime: getnowDate().endtimer,
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //dialog信息
      superDialog: {
        dialogVisible: false,
      },
      particularsDialog: {
        dialogVisible: false,
        id: "",
      },
      options: [
        {
          value: -1,
          label: "全部",
        },
        {
          value: 1,
          label: "待审核",
        },
        {
          value: 2,
          label: "已审核",
        },
      ],
      date: [],
    };
  },
  created() {
    this.search.created_date = this.normal_endtime;
    this.search.end_date = this.normal_endtime;
    if (this.normal_endtime) {
      this.date.push(this.search.end_date);
      this.date.push(this.search.end_date);
    }
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        code: this.search.code,
        status: this.search.status == -1 ? "" : this.search.status,

        created_date: this.search.created_date,
        end_date: this.search.end_date,
      };
      stockList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.data.forEach((item) => {
              switch (item.status) {
                case 1:
                  item.statusMsg = "待审核";
                  break;
                case 2:
                  item.statusMsg = "已审核";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //添加
    addPermission() {
      this.superDialog.dialogVisible = true;
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.date.length > 0) {
            this.search.created_date = this.date[0];
            this.search.end_date = this.date[1];
          }
          let status = this.search.status == -1 ? "" : this.search.status;
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Takestock/exportstockList?token=" +
            getToken() +
            "&status=" +
            status +
            "&created_date=" +
            this.search.created_date +
            "&end_date=" +
            this.search.end_date;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //审核
    modification(scope) {
      this.$confirm("是否审核该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: scope.row.id,
          };
          stockCheck(data).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
              this.getData();
            } else {
              this.$message({
                type: "info",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //删除
    remove(scope) {
      this.$confirm("是删除该盘点单?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: scope.row.id,
          };
          stockDelete(data).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
              this.getData();
            } else {
              this.$message({
                type: "info",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //详情
    particulars(scope) {
      this.particularsDialog.dialogVisible = true;
      this.particularsDialog.id = scope.row.id;
    },
    // 搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.created_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //清空
    empty() {
      this.search = {
        status: "",
        code: "",
        created_date: this.normal_endtime,
        end_date: this.normal_endtime,
      };
      this.date = [];
      this.date.push(this.search.end_date);
      this.date.push(this.search.end_date);
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },

    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  /* display: flex; */
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>